package cn.gogetter;

import java.io.File;
import java.util.Random;

import weka.core.Instances;
import weka.core.converters.ArffLoader;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.Add;
/**
 * 简单过滤器
 * 
 */
public class AddFiltering {
	public static void main(String[] args){
		ArffLoader arffloader = new ArffLoader();
		Instances data = null;
		Instances result = null;
		String path = new File("").getAbsolutePath()+"\\data\\";
		try {
			arffloader.setSource(new File(path+"weather.numeric.arff"));
			data = arffloader.getDataSet();
			Add filter; // 无监督的添加属性过滤器
			result = new Instances(data);
			// 新增数值属性
			filter = new Add();
			filter.setAttributeIndex("last"); // 设置所用属性的索引
			filter.setAttributeName("NumericAttribute"); // 设置新属性的名称
			filter.setInputFormat(result); // 设置输入实例的格式
			result = Filter.useFilter(result,filter);
			// 新增标称属性
			filter = new Add();
			filter.setAttributeIndex("last");
			filter.setNominalLabels("A,B,C"); // 设置标称属性的标签
			filter.setAttributeName("NominalAttribute");
			filter.setInputFormat(result);
			result = Filter.useFilter(result, filter);
			// 用随机值填充新增的两个属性
			Random rand = new Random(1234);
			for(int i = 0;i<result.numInstances();i++){
				result.instance(i).setValue(result.numAttributes() - 2, rand.nextDouble());
				result.instance(i).setValue(result.numAttributes() - 1, rand.nextInt(3));
			}
			// 输出数据
			System.out.println("过滤后的数据：");
			System.out.println(result);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
